{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "236704cc-3c74-48db-861f-38f81858c3dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "your name: Tom\n",
      "you are a boy?(y/n) y\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "authorizing....\n",
      "Welcome to the matrixMr.Tom.\n"
     ]
    }
   ],
   "source": [
    "name = input('your name:')\n",
    "gender = input('you are a boy?(y/n)') \n",
    "welcome_str = 'Welcome to the matrix{prefix}{name}.'\n",
    "welcome_dic = { 'prefix':'Mr.' if gender == 'y' else 'Mrs','name':name} \n",
    "print('authorizing....') \n",
    "print(welcome_str.format(**welcome_dic))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e4159365-ef98-4851-aa50-e77c7c4d0ceb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 需求统计txt所有英语单词的个数\n",
    "import re\n",
    "def parse(text): \n",
    "    # 使用正则表达式，移除标点符号以及换行符\n",
    "    text = re.sub(r'[^\\w]',' ',text)\n",
    "    # 转换为小写\n",
    "    text = text.lower()\n",
    "    # 空白字符进行分割\n",
    "    word_list = text.split(' ')\n",
    "    # 去除None内容,包括字符：0，{}，None \n",
    "    word_list = filter(None,word_list) \n",
    "    # 生成单词和词频字典\n",
    "    word_cnt = {}\n",
    "    for word in word_list:\n",
    "        # if word not in word_cnt:\n",
    "        #     word_cnt[word] = 0\n",
    "        #word_cnt[word] += 1\n",
    "        word_cnt[word] = word_cnt.get(word,0)+1\n",
    "        \n",
    "    sorted_word_cnt = sorted(word_cnt.items(),key=lambda kv:kv[1],reverse=True) # 按值内容进行排序，并实现降序\n",
    "    return sorted_word_cnt\n",
    "    \n",
    "    \n",
    "text = ''\n",
    "with open('./resource/in.txt','r') as f:\n",
    "    text = f.read()\n",
    "word_and_freq = parse(text)\n",
    "with open('./resource/out.txt','w') as f:\n",
    "    for word,freq in word_and_freq: \n",
    "        f.write('{} {} \\n'.format(word,freq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9fdec365-9018-4b06-af12-fa5a7637b49d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after json serialization\n",
      "type of params_str = <class 'str'>,params_str = {\"symbol\": \"123456\", \"type\": \"limit\", \"price\": 123.4, \"amount\": 23}\n",
      "after json deserialization\n",
      "type of params_str = <class 'dict'>,params_str = {'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}\n"
     ]
    }
   ],
   "source": [
    "#json序列化与实战\n",
    "import json\n",
    "params = { 'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}\n",
    "params_str = json.dumps(params) \n",
    "print('after json serialization') \n",
    "print('type of params_str = {},params_str = {}'.format(type(params_str),params_str))\n",
    "original_params = json.loads(params_str) \n",
    "print('after json deserialization') \n",
    "print('type of params_str = {},params_str = {}'.format(type(original_params),original_params))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bc88ba51-27d8-4797-9d11-ed5d680fd7fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type of params_str = <class 'dict'>,params_str = {'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "params = { 'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}\n",
    "# 写入json文件 \n",
    "with open('./resource/params.json','w') as fout:\n",
    "    json.dump(params,fout)\n",
    "# 读取json文件内容 \n",
    "with open('./resource/params.json','r') as fin:\n",
    "    original_params = json.load(fin)\n",
    "print('type of params_str = {},params_str = {}'.format(type(original_params),original_params))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0e60641-7fee-4912-b455-3bca3f2de878",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
